package exam.locadomain.server;

import exam.config.Utils;
import exam.locadomain.Content;
import exam.locadomain.Person;
import exam.locadomain.Title;
import exam.localdata.ListDataServer;
import exam.localdata.LocalPage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/3 15:41
 * @since JDK 1.8
 */
@Service
public class TitleServer {

    private static List<Content> contentList=new ArrayList<>();

    private static List<Title> titleList=new ArrayList<>();

    private static final String titleName="title";

    private static final String contentName="content";

    public List<Title> getTitleListPage(){
        return getTitleList();
    }

    public LocalPage<Title> getTitleListPage(Integer page,Integer pageSize,String title,String note){
        List<Title> titleList=getTitleList();
        List<Title> titleList1=titleList.stream().filter(e->e.getTitle().contains(title)||e.getNote().contains(note)
        ).collect(Collectors.toList());
        LocalPage<Title> titleLocalPage=ListDataServer.getLoclaPage(page,pageSize,titleList);
        return titleLocalPage;
    }


    public int addTitleContent(String title,String note,String content,String username,String uid){
        String uuid=Utils.getUUID();
        String createTime=Utils.getNowDate();

        Title title1=new Title();
        title1.setCreateDate(createTime);
        title1.setUuid(uuid);
        title1.setCreateUser(username);
        title1.setNote(note);
        title1.setCreateUserId(uid);
        title1.setTitle(title);

        Content content1=new Content();
        content1.setId(title1.getUuid());
        content1.setContent(content);

        addContent(content1);
        addTitle(title1);

        return 200;
    }



    public Map<String, Object> getContentById(String id){
        List<Content> contentList=getContentList();
        List<Title> titleList=getTitleList();
        contentList.stream().forEach(e->{
            System.out.println(e);
        });

        titleList.stream().forEach(e->{
            System.out.println(e);
        });
        Title title=titleList.stream().filter(e->e.getUuid().equals(id)).findFirst().get();
        Content content=contentList.stream().filter(e->e.getId().equals(id)).findFirst().get();
        Map<String, Object> objectMap=new HashMap<>();
        objectMap.put("id",id);
        objectMap.put("title",title.getTitle());
        objectMap.put("content",content.getContent());
        objectMap.put("createtime",title.getCreateDate());
        return objectMap;
    }



    private   Integer addTitle(Title title){
        if(titleList.size()==0){
            getTitleList();
        }
        titleList.add(title);
        ListDataServer.wirteList(titleName,titleList);
        return 200;
    }

    private Integer addContent(Content content){
       if(contentList.size()==0){
           getContentList();
       }
       contentList.add(content);
       ListDataServer.wirteList(contentName,contentList);
       return 200;
    }

    private List<Content> getContentList(){
        if(contentList.size()==0){
            contentList= ListDataServer.getListByName(contentName);
        }
        return contentList;
    }

    private List<Title> getTitleList(){
        if(titleList.size()==0){
            titleList= ListDataServer.getListByName(titleName);
        }
        return titleList;
    }
}
